package cn.tx.redis7.test;

import cn.tx.redis7.config.RedisUtils;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.otter.canal.client.CanalConnectors;
import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.protocol.Message;
import com.alibaba.otter.canal.protocol.CanalEntry.Column;
import com.alibaba.otter.canal.protocol.CanalEntry.Entry;
import com.alibaba.otter.canal.protocol.CanalEntry.EntryType;
import com.alibaba.otter.canal.protocol.CanalEntry.EventType;
import com.alibaba.otter.canal.protocol.CanalEntry.RowChange;
import com.alibaba.otter.canal.protocol.CanalEntry.RowData;
import redis.clients.jedis.Jedis;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author jizenan
 * @date 2025/5/30
 * Description:
 */
public class RedisCanalClientExample {
    public static final Integer _60SECONDS = 60;
    public static final String REDIS_IP_ADDR = "127.0.0.1";

    private static void redisInsert(List<Column> columns) {
        JSONObject jsonObject = new JSONObject();
        for (Column column : columns) {
            System.out.println(column.getName() + ":" +column.getValue() + " update=" + column.getUpdated());
            jsonObject.put(column.getName(),column.getValue());
        }
        if(columns.size() > 0){
            try(Jedis jedis = RedisUtils.getJedis()){
                jedis.set(columns.get(0).getValue(),jsonObject.toString());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private static void redisDelete(List<Column> columns) {
        JSONObject jsonObject = new JSONObject();
        for (Column column : columns) {
            jsonObject.put(column.getName(),column.getValue());
        }
        if(columns.size() > 0){
            try(Jedis jedis = RedisUtils.getJedis()){
                jedis.del(columns.get(0).getValue());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private static void redisUpdate(List<Column> columns) {
        JSONObject jsonObject = new JSONObject();
        for (Column column : columns) {
            System.out.println(column.getName() + ":" +column.getValue() + " update=" + column.getUpdated());
            jsonObject.put(column.getName(),column.getValue());
        }
        if(columns.size() > 0){
            try(Jedis jedis = RedisUtils.getJedis()){
                jedis.set(columns.get(0).getValue(),jsonObject.toString());
                System.out.println("-------update after: "+jedis.get(columns.get(0).getValue()));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private static void printEntry(List<Entry> entries) {
        for (Entry entry : entries) {
            if(entry.getEntryType() == EntryType.TRANSACTIONBEGIN || entry.getEntryType() == EntryType.TRANSACTIONEND){
                continue;
            }

            RowChange rowChange = null;
            try {
                //获取变更的row数据
                rowChange = RowChange.parseFrom(entry.getStoreValue());
            }catch (Exception e){
                throw new RuntimeException("error ## parser of eromanga-event has an error,data:"+entry.toString(),e);
            }
            //获取变动类型
            EventType eventType = rowChange.getEventType();
            System.out.println(String.format("=========&gt; binlog[%s:%s] , name[%s:%s] , eventType : %s",
                    entry.getHeader().getLogfileName(),entry.getHeader().getLogfileOffset(),
                    entry.getHeader().getSchemaName(),entry.getHeader().getTableName(),eventType));

            for (RowData rowData : rowChange.getRowDatasList()) {
                if (eventType == EventType.INSERT) {
                    redisInsert(rowData.getAfterColumnsList());
                } else if (eventType == EventType.DELETE) {
                    redisDelete(rowData.getBeforeColumnsList());
                } else {
                    redisUpdate(rowData.getAfterColumnsList());
                }
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("------------O(^_^)O哈哈~ initCanal() main 方法------------");
        CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress("192.168.0.137",11111),
                "example", "","");
        int batchSize = 0;
        //空闲空转计数器
        int emptyCount = 0;
        System.out.println("------------canal init OK，开始监听mysql变化");
        try{
            connector.connect();
            //connector.subscribe(".*\\..*");
            connector.subscribe("test.t_user");
            connector.rollback();
            int totalEmptyCount = 10 * _60SECONDS;
            while (emptyCount < totalEmptyCount){
                System.out.println("我是canal，每秒一次正在监听:"+ UUID.randomUUID().toString());
                Message message = connector.getWithoutAck(batchSize);//获取指定数量的数据
                long batchId = message.getId();
                int size = message.getEntries().size();
                if(batchId == -1 || size == 0){
                    emptyCount++;
                    try{
                        TimeUnit.SECONDS.sleep(1);}catch (InterruptedException e){e.printStackTrace();}
                }else{
                    //计数器重新置零
                    emptyCount = 0;
                    printEntry(message.getEntries());
                }
                connector.ack(batchId);
            }
            System.out.println("已经监听了"+totalEmptyCount+"秒，无任何消息，请重启重试.....");
        }finally {
            connector.disconnect();
        }
    }

}